package com.clkj.xiuzheng.service.shangcheng;

import com.clkj.xiuzheng.dao.ProductCommentImageMapper;
import com.clkj.xiuzheng.dao.shangcheng.ScCompanyMapper;
import com.clkj.xiuzheng.dao.shangcheng.ScProductcommentMapper;
import com.clkj.xiuzheng.pojo.common.DateFormatHelper;
import com.clkj.xiuzheng.pojo.common.ExcelRead;
import com.clkj.xiuzheng.pojo.common.OSSUtil;
import com.clkj.xiuzheng.pojo.common.UploadFile;
import com.clkj.xiuzheng.pojo.shangcheng.ProductComment;
import com.clkj.xiuzheng.pojo.shangcheng.ProductCommentImage;
import com.clkj.xiuzheng.pojo.shangcheng.ProductCompany;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class ScCommentServiceImpl implements ScCommentService{
    @Autowired
    private ScProductcommentMapper scProductcommentMapper;
    @Autowired
    private ProductCommentImageMapper productCommentImageMappe;

    @Override
    public PageInfo getProductByCategoryId(Integer productId, Integer commentType, Integer pageSize, Integer pageNum) {
        if (commentType == 0 || commentType == 1 || commentType == 2) {
            //获取所有好评 中评 差评
            List<ProductComment> productComments = scProductcommentMapper.getProductByType(productId,commentType);
            productComments.forEach(comment->{
                Example example1 = new Example(ProductCommentImage.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("commnetId",comment.getProductCommentId());
                List<ProductCommentImage> productCommentImages = productCommentImageMappe.selectByExample(example1);
                comment.setImageList(productCommentImages);
            });
            return new PageInfo(productComments);
        }
        else if (commentType == 3) {
            //获取有图
            List<ProductComment> productComments = scProductcommentMapper.getProductById(productId);
            List<ProductComment> productComments2 = new ArrayList<>();
            productComments.forEach(comment->{
                Example example1 = new Example(ProductCommentImage.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("commnetId",comment.getProductCommentId());
                List<ProductCommentImage> productCommentImages = productCommentImageMappe.selectByExample(example1);
                if (productCommentImages!=null && productCommentImages.size()!=0){
                    comment.setImageList(productCommentImages);
                    productComments2.add(comment);
                }
            });

            return new PageInfo(productComments2);
        }
        else if (commentType == 4) {
            //获取全部
            List<ProductComment> productComments = scProductcommentMapper.getProductById(productId);
            productComments.forEach(comment->{
                Example example1 = new Example(ProductCommentImage.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("commnetId",comment.getProductCommentId());
                List<ProductCommentImage> productCommentImages = productCommentImageMappe.selectByExample(example1);
                comment.setImageList(productCommentImages);
            });
            return new PageInfo(productComments);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, noRollbackFor = RuntimeException.class)
    public void addComment(ProductComment productComment) {
     //添加评论主信息
        productComment.setCommentDate(DateFormatHelper.today());
        scProductcommentMapper.addComment(productComment);
        List<ProductCommentImage> images = new ArrayList<ProductCommentImage>();
        ProductCommentImage productCommentImage=null;
        if(productComment.getImageList().size()>0){
            for (ProductCommentImage image : productComment.getImageList()) {
                //添加图片
                productCommentImage=   new ProductCommentImage();
                productCommentImage.setCommnetId(productComment.getProductCommentId());

                productCommentImage.setCommentImage(image.getCommentImage());
                images.add(productCommentImage);
            }
            productCommentImageMappe.insertList(images);

        }




    }

    @Override
    public PageInfo getCommentByCompanyId(Integer companyId,Integer productId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductComment> productComments = scProductcommentMapper.selectCommentByCompanyId(companyId,productId);
        productComments.forEach(comment->{
            Example example1 = new Example(ProductCommentImage.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("commnetId",comment.getProductCommentId());
            List<ProductCommentImage> productCommentImages = productCommentImageMappe.selectByExample(example1);
            comment.setImageList(productCommentImages);
        });
        return new PageInfo(productComments);
    }

    @Override
    public void deleteComment(String productCommentIds) {
        String[] ids = productCommentIds.split(",");
        Example example = new Example(ProductComment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("productCommentId", Arrays.asList(ids));
        scProductcommentMapper.deleteByExample(example);
    }

    @Override
    public void TopComment(String productCommentIds, Integer isTop) {
        String[] ids = productCommentIds.split(",");
        Example example = new Example(ProductComment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("productCommentId", Arrays.asList(ids));
        ProductComment productComment = new ProductComment();
        productComment.setIsTop(isTop);
        scProductcommentMapper.updateByExampleSelective(productComment,example);
    }

    @Override
    public void importComment(HttpServletRequest request) {

        try {
            MultipartFile file = ((MultipartRequest) request).getFile("file");
            File excelFile = null;
            excelFile = UploadFile.multipartFileToFile(file);
            List<Map<String, Object>> result = ExcelRead.ReadExcelByRC(excelFile, file.getContentType(), 2, -1, false);
            ProductComment productComment = null;
            List<ProductComment> productComments =new ArrayList<>();
            for (int i = 0; i < result.size(); i++) {
                if(result.get(i).get("1")==null||result.get(i).get("1").equals("null")){
                    break;
                }
                productComment = new ProductComment();
                productComment.setCommentUserId(Integer.parseInt(result.get(i).get("1").toString()));
                productComment.setCommentType(Integer.parseInt(result.get(i).get("2").toString()));
                productComment.setCommentContent(result.get(i).get("3").toString());
                productComment.setCommentDate(result.get(i).get("4").toString());
                productComment.setProductId(Integer.parseInt(result.get(i).get("6").toString()));
                productComment.setCompanyId(Integer.parseInt(result.get(i).get("7").toString()));
                productComment.setIsTop(Integer.parseInt(result.get(i).get("8").toString()));
                productComment.setCommentStatus("0");
                productComments.add(productComment);
            }
            if(productComments.size()>0){
                scProductcommentMapper.insertList(productComments);
            }
            file= null;
            excelFile= null;
            result = null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public String addCommentImg(HttpServletRequest request) {
        MultipartFile files = ((MultipartRequest) request).getFile("file");
        String img  = OSSUtil.uploadingImg(files,"xiuzheng/product/common");
        return img;
    }
}
